package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.myapplication.db.User;
import com.speedystone.greendaodemo.db.DaoSession;
import com.speedystone.greendaodemo.db.UserDao;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    UserDao userDao;

    Button save, query, dalete, update, btn;
    EditText age, name;


    DaoSession daoSession;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        daoSession = MyApp.getDaoSession();
        userDao = daoSession.getUserDao();
        init();



    }

    void init() {
        save = findViewById(R.id.btn_save);
        query = findViewById(R.id.btn_query);
        dalete = findViewById(R.id.btn_dalete);
        update = findViewById(R.id.btn_update);
        save.setOnClickListener(this);
        query.setOnClickListener(this);
        dalete.setOnClickListener(this);
        update.setOnClickListener(this);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(this);
        age = findViewById(R.id.userage);
        name = findViewById(R.id.username);

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                SaveDate();
                break;
            case R.id.btn_query:

                break;
            case R.id.btn_dalete:

                break;
            case R.id.btn_update:

                break;
            case R.id.btn:
                startActivity(new Intent(MainActivity.this, ListActivity.class));
                break;
        }
    }

    private void SaveDate() {
//        for (int i = 0; i <= 60; i++) {
//            userDao.insert(new User(null, "李林林" + i, 15));
//        }
        for (int i = 0; i <= 67; i++) {
            userDao.insert(new User(null, "李" + i, 10));
        }

        Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();

//        if (!TextUtils.isEmpty(age.getText().toString().trim()) && !TextUtils.isEmpty(name.getText().toString().trim())) {
//            User user = new User();
//            user.setAge(Integer.valueOf(age.getText().toString().trim()));
//            user.setName(name.getText().toString().trim());
//
//            long isSave = userDao.insert(user);
//            if (isSave >= 0) {
//                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
//            } else {
//                Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
//            }
//        } else {
//            Toast.makeText(MainActivity.this, "所有的必填", Toast.LENGTH_SHORT).show();
//        }
    }

    private List<User> QueryDate() {
        return userDao.queryBuilder().list();

    }

    private void DaleteDate() {


    }

    private void UpdateDate() {

    }

}